package 力扣日常刷题.木22一月.第12天0120;

/**
 * @author 帅小伙
 * @date 2022/1/20
 * @description
 * 两个状态的背包
 */
public class Demo35动态规划中等一和零 {


    public int findMaxForm(String[] strs, int m, int n) {
        int[][] dp = new int[m+1][n+1];
        int length = strs.length;

        for (int i = 0; i < length; i++) {
            int[] zerosOnes = getZerosOnes(strs[i]);
            int z = zerosOnes[0],o = zerosOnes[1];
            for (int j = m; j >=z ; j--) {
                for (int k = n; k >= o ; k--) {
                    dp[j][k] = Math.max(dp[j][k],dp[j-z][k-o] +1);
                }
            }
        }
        return dp[m][n];
    }


    int[] getZerosOnes(String str) {
        int[] zerosOnes = new int[2];
        for (char c : str.toCharArray()) {
            zerosOnes[c-'0']++;
        }
        return zerosOnes;
    }

}
